package com.zhida84.Servlet;
/**
 * 
 * @author shazhengbo sunchangxing
 *
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhida84.Convert.Base64CodeO;
import com.zhida84.Convert.JMMD5;
import com.zhida84.Dao.UserDao;
import com.zhida84.Entity.UserTab;
import com.zhida84.Impl.UserImpl;

/**
 * Servlet implementation class valUserServlet
 */
public class valUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public valUserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String UX = request.getParameter("phone");
		String array[] = UX.split(",");
		String userphone = array[0];
		String pwd = array[1];
		String phone = Base64CodeO.encoded(userphone);
		String uspwd = JMMD5.MD5(pwd);
		UserTab userTab = new UserTab();
		userTab.setUserPhone(phone);
		userTab.setUserPwd(uspwd);
		UserDao userDao = new UserImpl();
		ResultSet rs = userDao.valUserTab(userTab);
		try {
			if (rs.next()) {
				Cookie c1 = new Cookie("phone", phone);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Cookie c2 = new Cookie("lastVisited",
						sdf.format(new java.util.Date()));
				Cookie c3 = new Cookie("userId", String.valueOf(rs.getInt(1)));
				Cookie c4 = new Cookie("userRealName", rs.getString("userReName"));
				c1.setMaxAge(60 * 60 * 60 * 12 * 30);
				c2.setMaxAge(60 * 60 * 60 * 12 * 30);
				c3.setMaxAge(60 * 60 * 60 * 12 * 30);
				c4.setMaxAge(60 * 60 * 60 * 12 * 30);
				c1.setPath("/");
				c2.setPath("/");
				c3.setPath("/");
				c4.setPath("/");
				response.addCookie(c1);
				response.addCookie(c4);
				response.addCookie(c3);
				response.addCookie(c2);
				out.print("success");
			}
			else{
				out.print("default");
			}
			out.flush();
			out.close();
		} catch (Exception e) {
		}
	}

}
